package com.hh.admin.controller;

import com.hh.common.page.CommonPage;
import com.hh.common.request.BrokerageRecordRequest;
import com.hh.common.request.PageParamRequest;
import com.hh.common.request.merchant.AddMerchantRequest;
import com.hh.common.response.CommonResult;
import com.hh.common.response.merchant.MerchantListResponse;
import com.hh.service.service.merchant.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/admin/merchant")
public class MerchantController {

    @Autowired
    private MerchantService merchantService;

    /**
     * 商户列表
     * @param request
     * @param pageParamRequest
     * @return
     */
    @PreAuthorize("hasAuthority('admin:merchant:list')")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<MerchantListResponse>> getList(@Validated BrokerageRecordRequest request, @Validated PageParamRequest pageParamRequest){
        return CommonResult.success(CommonPage.restPage(merchantService.getList(request, pageParamRequest)));
    }

    /**
     * 添加商户
     * @param request
     * @return
     */
    @PreAuthorize("hasAuthority('admin:merchant:save')")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<Boolean> addMerchant(@RequestBody @Validated AddMerchantRequest request) {
        this.merchantService.addMerchant(request);
        return CommonResult.success();
    }
}
